# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openimageio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.4.7.1
pkgrel=1
pkgdesc="A library for reading and writing images, including classes, utilities, and applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.openimageio.org/"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-dcmtk"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-libheif"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libraw"
         "${MINGW_PACKAGE_PREFIX}-libsquish"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-opencolorio"
         "${MINGW_PACKAGE_PREFIX}-opencv"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openvdb"
         "${MINGW_PACKAGE_PREFIX}-ptex"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             "${MINGW_PACKAGE_PREFIX}-robin-map"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "${MINGW_PACKAGE_PREFIX}-python-numpy")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5-base: iv image viewer"
            "${MINGW_PACKAGE_PREFIX}-python: bindings support")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/OpenImageIO/oiio/archive/v${pkgver}.tar.gz
        0000-workaround-ansidecl-h-PTR-define-conflict.patch
        0001-Fix-building-on-mingw-w64-aarch64.patch)
sha256sums=('fd298f71e44c6776863db4b37c4a1388dba0d2eb37378afea95ab07a7cd6ecd4'
            '9e4e21333676268a91c0f4e7676aeab7658e5f748e7e5cfe43a92f0fd7931229'
            '91bd9a6a07f448a6dd8d1ff9a1c8cefaedb8cdd77029b84b6fc02fe9a7cf71f0')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${srcdir}/oiio-${pkgver}
  apply_patch_with_msg \
    0000-workaround-ansidecl-h-PTR-define-conflict.patch \
    0001-Fix-building-on-mingw-w64-aarch64.patch
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  local _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DUSE_FREETYPE=ON \
    -DUSE_OPENCV=ON \
    -DUSE_OPENGL=ON \
    -DUSE_QT=ON \
    -DUSE_FFMPEG=ON \
    -DPYTHON_VERSION=${_pyver} \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -DUSE_EXTERNAL_PUGIXML=ON \
    -DOIIO_BUILD_TESTS=OFF \
    -DBUILD_TESTING=OFF \
    -DSTOP_ON_WARNING=OFF \
    ${extra_config} \
    ../oiio-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -e "s|${PREFIX_WIN}|\$\{PACKAGE_PREFIX_DIR\}|g" \
    -i "${pkgdir}${MINGW_PREFIX}"/lib/cmake/OpenImageIO/OpenImageIOConfig.cmake
  sed -e "s|${MINGW_PREFIX}|\$\{PACKAGE_PREFIX_DIR\}|g" \
    -i "${pkgdir}${MINGW_PREFIX}"/lib/cmake/OpenImageIO/OpenImageIOConfig.cmake

  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/OpenImageIO/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm644 ${srcdir}/oiio-${pkgver}/LICENSE.md "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
